﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Projectmanagement.Utl;
using Projectmanagement.Entity;
using ProjectManagement.Models;

namespace Projectmanagement.Controllers
{
    [RoleRequired]
    public class TimeLineController : Controller
    {
        //
        // GET: /TimeLine/

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult freshTimeLine(string searchProject, string searchMember)
        {
            var tasks = from t in CH.DB.Tasks
                        where t.Project.IsActived == true && (t.TaskState.Code == 90 || t.TaskState.Code == 100 || t.TaskState.Code == 30) && t.MilestoneID != null
                        select t;
            if (!string.IsNullOrEmpty(searchProject))
            {
                var codes = searchProject.Split(new[] { ";", "；" }, StringSplitOptions.RemoveEmptyEntries);
                tasks = from t in tasks where codes.Contains(t.Project.ProjectCode) select t;
            }
            else
            {
                return PartialView("TimeLine");
            }
            if (!string.IsNullOrEmpty(searchMember))
            {
                tasks = from t in tasks where t.Owners.Contains(searchMember) select t;
            }
            if (Employee.CurrentRoleLVL < 100)
            {
                tasks = from t in tasks where t.Owners.Contains(Employee.CurrentUserName) select t;
            }
            return PartialView("TimeLine", tasks.GroupBy(g => g.Project.ProjectCode));
        }


        [HttpPost]
        public ActionResult GetRole()
        {
            var rolename = Employee.GetRoleName(Employee.CurrentUserName);
            return Json(rolename, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSelectedProjectMember(string name)
        {
            if (Employee.CurrentRoleLVL >= 100)
            {
                var members = GetTaskLineSelectMemberListItems(name);
                return Json(members, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var members = new List<SelectListItem>();
                members.Add(new SelectListItem()
                {
                    Text = Employee.CurrentUserName,
                    Value = Employee.CurrentUserName
                });
                return Json(members, JsonRequestBehavior.AllowGet);
            }
        }

        public IEnumerable<SelectListItem> GetTaskLineSelectMemberListItems(string name)
        {
            if (string.IsNullOrEmpty(name)) return null;
            string[] names = name.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            var ss = (from m in CH.DB.Members
                      join p in CH.DB.Projects on m.ProjectID equals p.ID
                      where names.Contains(p.ProjectCode) && m.IsActivated
                      select m.Name).Distinct().OrderBy(m => m);

            var selectList = ss.ToList().Select(a => new SelectListItem
            {
                Text = a,
                Value = a
            });
            return selectList;
        }
    }
}
